#include<stdio.h>
#include "config.h"
#include "parse_config.h"
#include "str.h"

static struct config_boolean{
	const char *config_key;
	int *config_value;
}config_boolen_array[] = {
	{
		"pasv_enable",&pasv_enable 
	},
	{
		"port_enable",&port_enable 
	},
	{
		NULL,
		NULL
	},
};

struct config_int{
	const char *config_key;
	int *config_value;
}config_int_array[] = {
	{
		NULL,NULL
	}
};

struct config_str{
	const char *config_key;
	const char **config_value;
}config_str_array[] = {
	{
		"home_path",
		 &home_path
	},
	{
		NULL,NULL
	}
};

void load_config(const char *config_path){
	//1
	FILE *file = fopen(config_path,"r");
	if( NULL == file){
		
	}
	char config_line[1024] = {0};
	while(fgets(config_line,sizeof(config_line),file) != NULL){
		if(strlen(config_line) == 0 || config_line[0] == '#' || str_all_space(config_line)){
			continue;
		}
		printf("%s",config_line);
		str_trim_crlf(config_line);
		parse_config(config_line);
		memset(config_line, 0, sizeof(config_line));
	}
	fclose(file);
}

void parse_config(const char *config_line){
	char *key[128] = {0};
	char *value[128] = {0};
	str_split(config_line,key,value,'=');
	if(strlen(value) == 0){
		
	}
	parse_int(key,value);
	parse_boolean(key,value);
	parse_str(key,value);
	
}

int parse_int(const char *key, char *value){
	const struct config_int *p_config_int = config_int_array;
	while (p_config_int->config_key != NULL) {
		if (strcmp(key, p_config_int->config_key) == 0) {
			if (value[0] == '0') {
				*(p_config_int->config_value) = str_octal_to_uint(value);
			} else {
				*(p_config_int->config_value) = atoi(value);
			}
			return;
		}
		p_config_int++;
	}
	
}

int parse_boolean(const char *key, char *value){
	
}

int parse_str(const char *key, char *value){
	const struct config_str *p_config_str = config_str_array;
	while (p_config_str->config_key != NULL) {
		if (strcmp(key, p_config_str->config_key) == 0) {
			printf("%s<----",p_config_str->config_value);
			const char **p_cur_value = p_config_str->config_value;
			if (*p_cur_value) {
				free((char*)*p_cur_value);
			}
			*p_cur_value = strdup(value);
		}
		p_config_str++;
	}
	return 1;
}


